#include "cJSON.h"
#include "log.h"
#include <stdlib.h>
int main(int argc, char const *argv[])
{
	//1  从json中提取值
	//创建一个json字符串 
	char *json = "{\"name\":\"zhangsan\",\"age\":18,\"sex\":\"man\"}";
	cJSON *root= cJSON_Parse(json); //解析json字符串
	 //判断是否解析e成功
	 if(root == NULL)
	 {
	 	log_error("json parse error\n");
	 	return -1;
	 }
	 cJSON *nameCjson = cJSON_GetObjectItem(root,"name");
	 cJSON *ageCjson = cJSON_GetObjectItem(root,"age");
	 cJSON *sexCjson = cJSON_GetObjectItem(root,"sex");
	 if(nameCjson == NULL || ageCjson == NULL || sexCjson == NULL)
	 {
	 	log_error("json parse error\n");
	 	return -1;
	 }
	 char *name= nameCjson->valuestring ;
	 int age = ageCjson->valueint;
	 char *sex = sexCjson->valuestring;
	 log_info("name:%s,age:%d,sex:%s",name,age,sex);

	//  2  通过值组合一个json字符串
	cJSON *root2 = cJSON_CreateObject();
	cJSON_AddStringToObject(root2,"name",name);
	cJSON_AddNumberToObject(root2,"age",age);
	cJSON_AddStringToObject(root2,"sex",sex);
	char *json2 = cJSON_PrintUnformatted(root2);
	log_info("json2:%s",json2);

     //3  释放内存
	 cJSON_Delete(root);
	 cJSON_Delete(root2);
	 free(json2);

	return 0;
}
